package com.yxh.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yxh.R;
import com.yxh.dto.LessonDto;
import com.yxh.util.Options;

/**
 * 课程Fragment
 * 
 * @author work
 * 
 */
public class ClassAdapter extends BaseAdapter {

	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	private List<LessonDto> list;
	private LayoutInflater inflater;

	public ClassAdapter(Context context, List<LessonDto> list) {
		this.list = list;
		this.inflater = LayoutInflater.from(context);
		this.imageLoader = ImageLoader.getInstance();
		if (!imageLoader.isInited()) {
			Options.initImageLoaderConfiguration(ImageLoader.getInstance(),
					context);
		}
		this.options = Options.getListOptions();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public LessonDto getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// CourseDto cDto = list.get(position);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_about_me_class_list_item, null);
			holder = new ViewHolder();
			holder.categoryTv = (TextView) convertView
					.findViewById(R.id.layout1_tv1);

			holder.layout = convertView.findViewById(R.id.layout2);
			holder.selectedImg = (ImageView) convertView
					.findViewById(R.id.img1);
			holder.courseImg = (ImageView) convertView.findViewById(R.id.img2);
			holder.titleTv = (TextView) convertView.findViewById(R.id.tv1);
			holder.contentTv = (TextView) convertView.findViewById(R.id.tv2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		LessonDto cDto = list.get(position);
		// LogUtil.e("isTitle :" + cDto.isTitle);
		if ("0".equals(cDto.isTitle)) {
			holder.categoryTv.setVisibility(View.VISIBLE);
			holder.categoryTv.setFocusable(false);
			holder.categoryTv.setClickable(true);
			holder.layout.setVisibility(View.GONE);
			if (cDto.state.equals("0")) {
				holder.categoryTv.setText("当前课程");
			} else if (cDto.state.equals("1")) {
				holder.categoryTv.setText("已完成");
			} else if (cDto.state.equals("2")) {
				holder.categoryTv.setText("已取消");
			}
		} else if ("1".equals(cDto.isTitle)) {
			holder.categoryTv.setVisibility(View.GONE);
			holder.layout.setVisibility(View.VISIBLE);
			if (!TextUtils.isEmpty(cDto.name))
				holder.titleTv.setText(cDto.name);

			if (!TextUtils.isEmpty(cDto.intro))
				holder.contentTv.setText(cDto.intro);

			if (!TextUtils.isEmpty(cDto.isDefault))
				holder.selectedImg
						.setVisibility(cDto.isDefault.equals("1") ? View.VISIBLE
								: View.GONE);

			if (!TextUtils.isEmpty(cDto.url))
				imageLoader.displayImage(cDto.url, holder.courseImg, options);
		}
		return convertView;
	}

	static class ViewHolder {
		TextView categoryTv;

		View layout;
		TextView titleTv;
		TextView contentTv;
		ImageView selectedImg;
		ImageView courseImg;

	}

}
